#include "lrcpage.h"
#include "ui_lrcpage.h"

LrcPage::LrcPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LrcPage)
{
    ui->setupUi(this);
   // this->setStyleSheet("border-image:url(\":/res/cover/lrcCover1.jpg\")");
    QPropertyAnimation *anime=new QPropertyAnimation(this,"geometry",this);
    anime->setDuration(300);
    anime->setStartValue(QRect(0,0,width(),height()));
    anime->setEndValue(QRect(0,width(),width(),height()));

    connect(ui->dropDownBtn,&QPushButton::clicked,this,[=](){
        anime->start();
    });

    connect(anime,&QPropertyAnimation::finished,this,[=](){
        this->hide();
    });

}

LrcPage::~LrcPage()
{
    delete ui;
}

bool LrcPage::parseLrcFile(const QString lrcPath)
{
    this->lrcWordLines.clear();
    QFile lrcFile(lrcPath);
    lrcFile.open(QIODevice::ReadOnly);
    if(!lrcFile.isOpen())
    {
        qDebug()<<"歌词文件: "<<lrcPath<<" 打开失败";
        this->lrcWordLines.push_back({0,"暂无歌词"});
        return false;
    }


    while(!lrcFile.atEnd())
    {
        //读取歌词文件
        QString lrcLine=lrcFile.readLine(1024);
        if(lrcLine<=0)
        {
            qDebug()<<"歌词文件: "<<lrcPath<<" 读取失败";
            continue;
        }
        int start=0,end=0;
         end=lrcLine.indexOf(']',start);
         QString lrcTime=lrcLine.mid(start,end-start+1);
         QString lrcText=lrcLine.mid(end+1,lrcLine.size()-end-1-1);  //正则表达式
         //qDebug()<<lrcTime<<" ";
         //解析时间和歌词
         qint64 time=0;
         start=1;
         end=lrcTime.indexOf(':',start);
         time+=lrcTime.mid(start,end-start).toInt()*60*1000;   //解析分

         start=end+1;
         end=lrcTime.indexOf('.',start);
         time+=lrcTime.mid(start,end-start).toInt()*1000;   //解析秒

         start=end+1;
         end=lrcTime.indexOf('.',start);
         time+=lrcTime.mid(start,end-start).toInt();

         this->lrcWordLines.push_back(LrcWordLine(time,lrcText));
    }

//    for(auto lrc:lrcWordLines)
//    {
//        qDebug()<<lrc.time<<":"<<lrc.lrcText;
//    }

    lrcFile.close();

    return true;
}

void LrcPage::showLrcWordLine(qint64 time)
{
    int index=getLrcWordLineIndex(time);
    if(index==-1)
    {
        ui->lrcText1->setText("");
        ui->lrcText2->setText("");
        ui->lrcText3->setText("");
        ui->lrcTextCenter->setText("暂无歌词");
        ui->lrcText5->setText("");
        ui->lrcText6->setText("");
        ui->lrcText7->setText("");
    }
    else
    {
        ui->lrcText1->setText(getLrcWordByIndex(index-3));
        ui->lrcText2->setText(getLrcWordByIndex(index-2));
        ui->lrcText3->setText(getLrcWordByIndex(index-1));
        ui->lrcTextCenter->setText(getLrcWordByIndex(index));
        ui->lrcText5->setText(getLrcWordByIndex(index+1));
        ui->lrcText6->setText(getLrcWordByIndex(index+2));
        ui->lrcText7->setText(getLrcWordByIndex(index+3));
    }
}

int LrcPage::getLrcWordLineIndex(qint64 time)
{
    if(this->lrcWordLines.empty())
        return -1;
    if(time<=this->lrcWordLines[0].time)
        return 0;

    for(int i=1;i<this->lrcWordLines.size();i++)
    {
        if(time>=lrcWordLines[i-1].time&&time<lrcWordLines[i].time)
            return i-1;
    }

    return this->lrcWordLines.size()-1;
}

QString LrcPage::getLrcWordByIndex(int index)
{
    if(index<0)
        return this->lrcWordLines[0].lrcText;

    if(index>=this->lrcWordLines.size())
        return this->lrcWordLines[lrcWordLines.size()-1].lrcText;

    return lrcWordLines[index].lrcText;
}

void LrcPage::setNameAndSinger(const QString name, const QString singer)
{
    ui->musicName->setText(name);
    ui->musicSinger->setText(singer);
}





























